package com.gxa.controller.wgl.customer;


import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.common.vo.PatientVo;
import com.gxa.entity.CustomerManagement;
import com.gxa.service.CustomerManagementService;
import com.gxa.service.PatientManagementService;
import com.gxa.utils.HeaderUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


/**
 * 客服管理
 * 杨鸿宇
 */
@Controller
@RequestMapping(value = "/customer/customerService")
@RequiresRoles(value = {"超级管理员","院长","主任","秘书"},logical = Logical.OR)
@Api(value = "客服管理",tags = "客服管理接口API")
public class CustomerManagementController {

@Autowired
    CustomerManagementService customerManagementService;
@Autowired
PatientManagementService patientManagementService;
    /**
     * 查找所有客服消息
     * @param currentPage 当前页
     * @param pageSize 页大小
     * @return
     */
    @RequestMapping(value = "/findServiceList",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有客服消息",notes = "查询所有客服消息",httpMethod = "GET", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码数默认第一页",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页显示记录数",paramType = "query",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
            })
    @ResponseBody
    public R findServiceList(Integer currentPage,Integer pageSize){

        R r = new R<>();

        if (currentPage != null && pageSize != null){
        PageInfo<CustomerManagement> customerManagementList = customerManagementService.getCustomerManagementList(currentPage, pageSize);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(customerManagementList);
        }else {
            r.fromStatus(Status.BAD_PAGE_PARAM);
        }

        return r;



    }

    /**
     * 添加一条客服消息
     * @param customer 封装为一个客户消息实体
     * @return
     */
    @RequestMapping(value = "/addService",method = RequestMethod.POST)
    @ApiOperation(value = "添加一条客服消息",notes = "添加一条客服消息",httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @ResponseBody
    public R addService(@RequestBody @ApiParam(name = "customer",value = "消息实体",required = true) CustomerManagement customer,
                        HttpServletRequest request){
        R r = new R();
        Integer userId = HeaderUtils.getUserID(request);

        if(customer!=null){
            boolean c = customerManagementService.addCustomerManagement(customer);
            customer.setUserId(userId);
            if(c == true){
                r.setCode(200);
                r.setMsg("success");
            }
        }else{
            r.setCode(500);
            r.setMsg("添加失败");
        }
        return r;
    }

    /**
     * 修改一条客服消息
     * @param customer 封装为一个客户消息实体
     * @return
     */
    @RequestMapping(value = "/updateService",method = RequestMethod.POST)
    @ApiOperation(value = "更新一条客服消息",notes = "更新一条客服消息",httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @ResponseBody
    public R updateService(@RequestBody @ApiParam(name = "customer",value = "消息实体",required = true)CustomerManagement customer){
        R r = new R();
        if(customer !=null){
            customerManagementService.updateCustomerManagement(customer);
            return  R.ok();
        }else {
            r.setCode(400);
            r.setMsg("更新失败");
        }

        return r;
    }

    /**
     * 通过标题查询客服消息
     * @param title 标题
     * @return
     */
    @RequestMapping(value = "/getServiceByTitle",method = RequestMethod.GET)
    @ApiOperation(value = "标题查询客服消息",notes = "标题查询客服消息",httpMethod = "GET", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码数默认第一页",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页显示记录数",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "title",value = "标题",paramType = "query",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @ResponseBody
    public R getServiceByTitle(@RequestParam("title") String title){

        R r = new R();
        if(title !=null) {
            PageInfo<CustomerManagement> getByTitle = customerManagementService.getCustomerManagementListByTitle(title);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(getByTitle);
        }else {
            r.fromStatus(Status.BAD_PAGE_PARAM);
        }
        return r;
    }

    /**
     * 删除一条客服消息
     * @param id 消息编号
     * @return
     */
    @RequestMapping(value = "/deleteService",method = RequestMethod.POST)
    @ApiOperation(value = "删除一条客服消息",notes = "删除一条客服消息",httpMethod = "POST", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",paramType = "query",dataType = "String",required = true),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @ResponseBody
    public R deleteService(Integer id){

        R r = new R();
        if(id!= 0 && id>=0){
            boolean c = customerManagementService.delCustomerManagement(id);
            if(c == true){
                return R.ok();
            }

        }
        return r;
    }


    /**
     * 通过患者id获取患者消息
     * @param id 患者编号
     * @return
     */
    @RequestMapping(value = "/getPatientById",method = RequestMethod.GET)
    @ApiOperation(value = "获取患者name",notes = "通过所有患者获取单个患者",httpMethod = "GET", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",paramType = "query",dataType = "String",required = true),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @ResponseBody
    public R getPatientById(@RequestParam("id") Integer id){
        R r = new R<>();

        PatientVo patientById = patientManagementService.getPatientById(id);

        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(patientById);
        return r;
    }

    /**
     * 根据id查询客服信息
     * @param id 客服id
     * @return
     */
    @RequestMapping(value = "/getServiceById",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询客服",notes = "查询对应id的客服消息",httpMethod = "GET", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",paramType = "query",dataType = "String",required = true),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @ResponseBody


    public R getCustomerManagementById(Integer id){

        R r = new R();

        CustomerManagement getCustomerManagementById = customerManagementService.getCustomerManagementById(id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(getCustomerManagementById);
        return r;
    }


}
